/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-11-25     86159       the first version
 */

#include"event.h"
void th111_entry(void *parameter)
{
    while(1)
    {
        rt_event_recv(event1, EVENT_FLAGS_1, RT_EVENT_FLAG_CLEAR|RT_EVENT_FLAG_AND, RT_WAITING_FOREVER, RT_NULL);
        rt_kprintf("th1_entry...\n");
        rt_event_send(event1, EVENT_FLAGS_2);
        rt_thread_mdelay(1000);
    }
}
void th222_entry(void *parameter)
{
    while(1)
    {
        rt_event_recv(event1, EVENT_FLAGS_2, RT_EVENT_FLAG_CLEAR|RT_EVENT_FLAG_AND, RT_WAITING_FOREVER, RT_NULL);
        rt_kprintf("th2_entry...\n");
        rt_event_send(event1, EVENT_FLAGS_3);
        rt_thread_mdelay(1000);
    }
}
void th333_entry(void *parameter)
{
    while(1)
    {
        rt_event_recv(event1, EVENT_FLAGS_3, RT_EVENT_FLAG_CLEAR|RT_EVENT_FLAG_AND, RT_WAITING_FOREVER, RT_NULL);
        rt_kprintf("th3_entry...\n");
        rt_event_send(event1, EVENT_FLAGS_1);
        rt_thread_mdelay(1000);
    }
}
int event(void)
{
    /*
     * rt_event_creat/init();  创建  初始化
     * rt_event_send();   发送
     * rt_event_recv();   接受
     * rt_event_delete/detach();  删除  脱离
     */

    event1=rt_event_create("set1", RT_IPC_FLAG_FIFO);
    if(event1==RT_NULL)
    {
        LOG_E("rt_event_creat failed...\n");

    }
    LOG_D("rt_event_creat successed...\n");

    th11=rt_thread_create("th1", th111_entry, RT_NULL, 512, 10, 5);
    if(th11==RT_NULL)
        {
            LOG_E("th1 rt_thread_create failed...\n");
            return -ENOMEM;
        }

    th22=rt_thread_create("th2", th222_entry, RT_NULL, 512, 10, 5);
    if(th22==RT_NULL)
    {
         LOG_E("th2 rt_thread_create failed...\n");
         return -ENOMEM;
    }
    th33=rt_thread_create("th3", th333_entry, RT_NULL, 512, 10, 5);
    if(th33==RT_NULL)
    {
         LOG_E("th3 rt_thread_create failed...\n");
         return -ENOMEM;
    }
    rt_thread_startup(th11);
    rt_thread_startup(th22);
    rt_thread_startup(th33);

    rt_event_send(event1, EVENT_FLAGS_1);
    return 0;
}
